<template>
  <div class="container">
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-select
            v-model="searchObj.provinceCode"
            @change="provinceChanged"
            placeholder="请选择省"
          >
            <el-option
              v-for="item in provinceList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchObj.cityCode"
            @change="cityChanged"
            placeholder="请选择市"
          >
            <el-option
              v-for="item in cityList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="医院名称" />
        </el-form-item>
        <el-button type="primary">查询</el-button>
        <el-button type="danger">清空</el-button>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highligh-current-row
    >
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="医院logo">
        <template slot-scope="scope">
          <img :src="`data:image/jpeg;base64,${scope.row.logoData}`" width="80">
        </template>
      </el-table-column>

      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="param.hostypeString" label="等级" width="90" />
      <el-table-column prop="param.fullAddress" label="详情地址" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 0 ? "未上线" : "已上线" }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />

      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            type="primary"
            size="mini"
            @click="updateStatus(scope.row.id, 0)"
            >下线</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            type="danger"
            size="mini"
            @click="updateStatus(scope.row.id, 1)"
            >上线</el-button
          >
          <router-link :to="'/hospSet/hospital/show/' + scope.row.id">
            <el-button type="primary" size="mini">查看</el-button>
          </router-link>
          <router-link :to="'/hospSet/hospital/schedule/' + scope.row.hoscode">
            <el-button type="primary" size="mini">排班</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[10, 30, 60, 90]"
      style="padding: 30px 0; text-align: center"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"   
    />
  </div>
</template>

<script>
import hosp from '@/api/hosp/hosp';
export default {
  name: "List",
  data() {
    return {
      page: 1,
      total: 0,
      limit: 10,
      listLoading: true,
      list: null,
      searchObj: {},
      provinceList: [],
      cityList: [],
      districtList: [],
    };
  },
  created() {
    this.fetchData()
    this.findAllProvince()
  },
  methods: {
    updateStatus(id, status) {
      hosp.updateStatus(id, status).then((res) => {
        this.fetchData(this.page)
      }).catch((err) => {
        console.log(err);
      })
    },
    fetchData(page=1) {
      this.page = page
      hosp.getHospList(this.page, this.limit, this.searchObj).then((res) => {
        this.list = res.data.content
        this.total = res.data.totalElements
        this.listLoading = false
      }).catch((error) => {
        console.log(error);
      })
    },
    findAllProvince() {
      hosp.findByDictCode('Province')
        .then((res) => {
          this.provinceList = res.data
        }).catch((err) => {
          console.log(err);
        })
    },
    provinceChanged() {
      this.cityList = []
      this.searchObj.cityCode = null
      this.districtList = []
      this.searchObj.districtCode = null
      hosp.findChildId(this.searchObj.provinceCode).then((res) => {
        this.cityList = res.data
        console.log(this.cityList);
      })
    },
    cityChanged() {
     this.$forceUpdate()
    },
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },
  },
};
</script>

<style lang="less"></style>
